<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Repository\Product\GoodsRepository;
use App\Repository\UpThree\ThreeOrderRepository;
use App\Repository\UpThree\MainOrderRepostition;
use Illuminate\Support\Facades\DB;
use App\Repository\Product\SetAddressRepository;
use App\Repository\Product\ProductRepository;
use App\Repository\Api\GoodsRepository as apiGoods;

/**
 * @OA\Tag(
 *     name="后台菜单模块",
 *     description="菜单模块",
 * )
 */
class AppGoodsController extends BaseController
{
  private $model = null;
  private $addressModel;
  private $productModel;
  private $apiGoodsModel;
  public function __construct(GoodsRepository $GoodsRepository, SetAddressRepository $SetAddressRepository, apiGoods $apiGoods)
  {
    $this->model = $GoodsRepository;
    $this->addressModel = $SetAddressRepository;
    $this->productModel = new ProductRepository();
    $this->apiGoodsModel = $apiGoods;
  }
  public function getGoodsList(Request $request)
  {

    $shop_id = $request->get('shopId');
    if (empty($shop_id)) {
      return $this->returnMsg(100, '参数错误,请确认正确的连接');
    }

    $url = $_SERVER['HTTP_HOST'];
    // var_dump($url);
    if (!empty($url) && $url == 'stockapis.chenqiwangluo.com') {
      $url = 'adminnew.chenqiwangluo.com';
    }
    $path = 'http://';
    if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ||  $_SERVER['SERVER_PORT'] == '443') {
      $path = 'https://';
    }

    // $shop_id = 1;
    $where = [
      'where' => [
        'shop_id' => $shop_id,
        // 'is_app' => 0,
        'is_app_all' => 0,
        'status' => 0
        // 'is_applets' => 0
      ],
      'limit' => [
        'start' => 0,
        'number' => 20
      ],
      'order' => [
        'orderby' => ['sort' => 'asc', 'id' => 'desc'],
        // 'orderdir' => 'desc'
      ]
    ];
    if (!empty($request->get('search'))) {
      $where['like']['name'] = $request->get('search');
    }
    // var_dump($where);
    $allData = $this->model->index($where);
    // 将图片 处理成 绝对路径
    // if (!empty($allData['data'])) {
    //   foreach ($allData['data'] as $key => &$value) {
    //     $value['mainPic'] = $path . $url . $value['mainPic'];
    //     // $value['main_pic'] = $path . $url . $value['main_pic'];
    //     $value['nums'] = mt_rand(10, 99) / 10;
    //   }
    // }
    // return $allData;
    $return = [];
    $returns = [];
    if (!empty($allData['data'])) {
      foreach ($allData['data'] as $key => $v) {
        $v['mainPic'] = $path . $url . $v['mainPic'];
        $v['surplur'] = mt_rand(1000, 99999);
        $v['sale_nums'] = mt_rand(1, 5);
        if (empty($v['sort'])) {
          $returns[] = $v;
        }
        if (!empty($v['sort'])) {
          $return[] = $v;
        }
      }
    }
    // var_dump($return, 111111111);
    // var_dump($returns, 2222222222);
    $return = array_merge($return, $returns);
    return $this->returnMsg(200, '查询成功', $return);
    return $allData;
  }
  //获取商品信息
  public function getGoods(Request $request)
  {
    $url = $_SERVER['HTTP_HOST'];
    if (!empty($url) && $url == 'stockapis.chenqiwangluo.com') {
      $url = 'adminnew.chenqiwangluo.com';
    }
    // var_dump($url);
    $path = 'http://';
    if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ||  $_SERVER['SERVER_PORT'] == '443') {
      $path = 'https://';
    }
    $param = $request->all();
    $data = $this->model->getOne($param);

    if (!empty($data['goods_detail'])) {
      $data['goods_detail'] = htmlspecialchars_decode($data['goods_detail']);
    }
    if (!empty($data['main_pic'])) {
      $data['main_pic'] = $path . $url . $data['main_pic'];
    }
    return  $this->returnMsg(200, '欢迎下单', $data);
  }
  // 发货地址 传递过来的产品id
  public function getGoodsAddress(Request $request)
  {
    // $where = [];
    // $provinces = DB::table('address_provinces')->where($where)->get();
    // $city = DB::table('address_cities')->where($where)->get();
    // $area = DB::table('address_areas')->where($where)->get();
    // $productId = $request->get('id');
    $goodsId = $request->get('id');
    if (!empty($request->get('product_id'))) {
      $productId = $request->get('product_id');
    }
    if (empty($productId)) {
      $resGoods = $this->model->getOne(['id' => $goodsId], ['id', 'sup_goods_id_str']);
      if (!$resGoods) {
        return $this->returnMsg(101, '未查询到商品信息');
      }
      if (empty($resGoods->sup_goods_id_str)) {
        return $this->returnMsg(102, '未查询号码信息');
      }
      $prodArr = explode('.', $resGoods->sup_goods_id_str);
      $goodsId = $prodArr[1];
    }
    $resGoods = $this->model->getOne(['id' => $goodsId], ['id', 'product_id']);
    if (empty($resGoods->product_id)) {
      return $this->returnMsg(103, '未查询到产品');
    }
    $productId = $resGoods->product_id;
    // DB::table()->
    $wheres = [
      'id' => $productId,
    ];
    $Product = $this->productModel->getOne($wheres, ['id', 'product_address']);
    if (!$Product) {
      // 没查询到配置的话 直接全国
      return $this->returnMsg(100, '未查询到产品信息',);
    }
    // var_dump($Product);
    // 通过产品id 然后查询到归属地
    $where = [
      'id' => $Product->product_address
    ];
    $addressArr = $this->addressModel->getOne($where, ['id', 'name', 'type', 'province_city']);
    // if (empty($addressArr->province_city)) {
    //   return $this->returnMsg(100, '未查询到收货地');
    // }

    // 查询地址
    $province = [];
    $city = [];
    if (!empty($addressArr->province_city)) {
      $arr = json_decode($addressArr->province_city, true);

      foreach ($arr as $key => $v) {
        $province[] = $v[0];
        if (!empty($v[1])) {
          $city[] = $v[1];
        }
      }
    }

    return $this->apiGoodsModel->getAppAddress($province, $city, true, !empty($addressArr->type) ? $addressArr->type : 0);
    // return $this->getAddress([], []);
  }
  // 获取号码的归属地  传递过来的是产品id
  public function getOwnerAddress(Request $request)
  {
    $goodsId = $request->get('id');
    if (!empty($request->get('product_id'))) {
      $productId = $request->get('product_id');
    }
    if (empty($productId)) {
      $resGoods = $this->model->getOne(['id' => $goodsId], ['id', 'sup_goods_id_str']);
      if (!$resGoods) {
        return $this->returnMsg(101, '未查询到商品信息');
      }
      if (empty($resGoods->sup_goods_id_str)) {
        return $this->returnMsg(102, '未查询号码归属地');
      }
      $prodArr = explode('.', $resGoods->sup_goods_id_str);
      $productId = $prodArr[1];
    }
    // DB::table()->
    $wheres = [
      'id' => $productId,
    ];
    $Product = $this->productModel->getOne($wheres, ['id', 'product_address']);
    if (!$Product) {
      return $this->returnMsg(100, '未查询到产品信息');
    }
    // var_dump($Product);
    // 通过产品id 然后查询到归属地
    $where = [
      'id' => $Product->product_address
    ];
    $addressArr = $this->addressModel->getOne($where, ['id', 'name', 'owner']);
    if (empty($addressArr->owner)) {
      return $this->returnMsg(100, '未查询到归属地');
    }
    // 查询地址
    $arr = json_decode($addressArr->owner, true);
    $province = [];
    $city = [];
    foreach ($arr as $key => $v) {
      $province[] = $v[0];
      $city[] = $v[1];
    }
    return $this->apiGoodsModel->getAddress($province, $city, false);
    // return $this->returnMsg(200, '查询成功', json_decode($addressArr->owner));
  }

  // 获取号码
  public function getSelectNumber(Request $request)
  {
    $param = $request->all();
    $goodsId = $request->get('id');
    if (!empty($request->get('product_id'))) {
      $productId = $request->get('product_id');
    }
    if (empty($productId)) {
      $resGoods = $this->model->getOne(['id' => $goodsId], ['id', 'sup_goods_id_str']);
      if (!$resGoods) {
        return $this->returnMsg(101, '未查询到商品信息');
      }
      if (empty($resGoods->sup_goods_id_str)) {
        return $this->returnMsg(102, '未查询号码信息');
      }
      $prodArr = explode('.', $resGoods->sup_goods_id_str);
      $goodsId = $prodArr[1];
    }
    $resGoods = $this->model->getOne(['id' => $goodsId], ['id', 'product_id']);
    if (empty($resGoods->product_id)) {
      return $this->returnMsg(102, '未查询到产品');
    }
    $productId = $resGoods->product_id;
    // $productArr = $this->productModel->getOne($wheres, ['id', 'channel_id']);
    // $data = 'numbercity';
    $data = $this->apiGoodsModel->getSelectNumber($productId, $param);
    return $data;
    // if (empty($data)) {
    //   return $this->returnMsg(100, '查询失败', []);
    // }
    // // $datas = json_decode($data, true);
    // return $this->returnMsg(200, '查询成功', $data);
  }
  // 查询通道的号码
}
